Time time ago an gif appears showing the change of the global temperatures over time.

Well, some sites like http://gizmodo.com/ made a reference to this animation as one-of-the-most-convincing-climate-change-visualization. Mmmm… ok! A kind of click bait IMHO but at least the title saids visualization :B. But for me the animation don’t work always. I rembember a quote, sadly I don’t rember the author, may be/surely was Alberto Cairo (If you know it please tell me who was):

Animation force the user to compare what they see with what they remember (saw).

If you want it in Yoda’s way:

Other thing I don’t like so much about this spiral is there’are so much data overlaped missing information about the speed of increment in the temperatures.

So this post will be about if we can show this data in other ways to try to tell more clearly the Oh! Foo! is this rly happening? story.

Data & Packages

We’ll use the data provide by hrbrmstr in his repo. Bob Rudis made a beautiful representation of the data via ggplot2 and D3 using a geom_segment/column range viz.

About the packages. Here we’ll use a lot of dplyr, tidyr, purrr for the data manipulation, for the colors we’ll use viridis and last, for the charts highcharter

library("highcharter")
library("readr")
library("dplyr")
library("tidyr")
library("lubridate")
library("purrr")
library("viridis")

options(
  highcharter.theme = hc_theme_darkunica(
    chart  = list(style = list(fontFamily = "Roboto Condensed")),
    plotOptions = list(series = list(showInLegend = FALSE))
  )
)

df <- read_csv("https://raw.githubusercontent.com/hrbrmstr/hadcrut/master/data/temps.csv")

df <- df %>% 
  mutate(date = ymd(year_mon),
         tmpstmp = datetime_to_timestamp(date),
         year = year(date),
         month = month(date, label = TRUE),
         color_m = colorize(median, viridis(10)),
         color_m = hex_to_rgba(color_m, 0.65))

dfcolyrs <- df %>% 
  group_by(year) %>% 
  summarise(median = median(median)) %>% 
  ungroup() %>% 
  mutate(color_y = colorize(median, viridis(10)),
         color_y = hex_to_rgba(color_y, 0.65)) %>% 
  select(-median)

df <- left_join(df, dfcolyrs, by = "year")

The data is ready, let’s go.

year_mon median lower upper year decade month date tmpstmp color_m color_y
1850-01-01 -0.702 -1.102 -0.299 1850 1850 Jan 1850-01-01 -3.786826e+12 rgba(68,5,87,0.65) rgba(58,82,138,0.65)
1850-02-01 -0.284 -0.675 0.114 1850 1850 Feb 1850-02-01 -3.784147e+12 rgba(52,95,140,0.65) rgba(58,82,138,0.65)
1850-03-01 -0.732 -1.080 -0.383 1850 1850 Mar 1850-03-01 -3.781728e+12 rgba(68,3,86,0.65) rgba(58,82,138,0.65)
1850-04-01 -0.570 -0.903 -0.237 1850 1850 Apr 1850-04-01 -3.779050e+12 rgba(69,14,96,0.65) rgba(58,82,138,0.65)
1850-05-01 -0.325 -0.662 0.006 1850 1850 May 1850-05-01 -3.776458e+12 rgba(59,80,138,0.65) rgba(58,82,138,0.65)
1850-06-01 -0.213 -0.515 0.084 1850 1850 Jun 1850-06-01 -3.773779e+12 rgba(42,120,142,0.65) rgba(58,82,138,0.65)

Spiral

First of all let’s try to replicate the chart/gif/animation that’s reason to write this post. Here we’ll construtc a list of series to use with hc_add_series_list function.

lsseries <- df %>% 
  group_by(year) %>% 
  do(
    data = .$median,
    color = first(.$color_y)) %>% 
  mutate(name = year) %>% 
  list.parse3()

hc1 <- highchart() %>% 
  hc_chart(polar = TRUE) %>% 
  hc_plotOptions(series = list(marker = list(enabled = FALSE), animation = TRUE, pointIntervalUnit = "month")) %>%
  hc_legend(enabled = FALSE) %>% 
  hc_xAxis(type = "datetime", min = 0, max = 365 * 24 * 36e5, labels = list(format = "{value:%B}")) %>%
  hc_tooltip(headerFormat = "{point.key}", xDateFormat = "%B", pointFormat = " {series.name}: {point.y}") %>% 
  hc_add_series_list(lsseries)

hc1

Ok! without the animation componet this don’t work so much.

Spiral w/animation

If we want replicate the animation part we can hide all the series using transparency.

lsseries2 <- df %>% 
  group_by(year) %>% 
  do(
    data = .$median,
    color = "transparent",
    enableMouseTracking = FALSE,
    color2 = first(.$color_y)) %>% 
  mutate(name = year) %>% 
  list.parse3()

Then using a little of javascript we can color each series one by one with the real color.

hc11 <- highchart() %>% 
  hc_chart(polar = TRUE) %>% 
  hc_plotOptions(series = list(
    marker = list(enabled = FALSE),
    animation = TRUE,
    pointIntervalUnit = "month")) %>%
  hc_legend(enabled = FALSE) %>% 
  hc_xAxis(type = "datetime", min = 0, max = 365 * 24 * 36e5,  labels = list(format = "{value:%B}")) %>%
  hc_tooltip(headerFormat = "{point.key}", xDateFormat = "%B", pointFormat = " {series.name}: {point.y}") %>% 
  hc_add_series_list(lsseries2) %>% 
  hc_chart(
    events = list(
      load = JS("

function() {
  console.log('ready');
  var duration = 16 * 1000
  var delta = duration/this.series.length;
  var delay = 0;

  this.series.map(function(e){
    setTimeout(function() {
      e.update({color: e.options.color2, enableMouseTracking: true});
      e.chart.setTitle(null, {text: e.name})
    }, delay)
    delay = delay + delta;
  });
}
                ")
    )
  )

# rm theme
hc11$x$theme <- list(chart = list(divBackgroundImage = NULL))

And voilĂ 

hc11

You can open the chart in a new window to see the animation effect.

Sesonalplot

We need polar coords here? I don’t know so let’s back to the euclidean space and see what happend

hc2 <- hc1 %>% 
  hc_chart(polar = FALSE, type = "spline") %>% 
  hc_xAxis(max = (365 - 1) * 24 * 36e5)

hc2

Ñom! A nice colored spaghettis. Not so much clear what happended across the years.

Heatmap

Here we put the years in xAxis and month in yAxis:

m <- df %>% 
  select(year, month, median) %>% 
  spread(year, median) %>% 
  select(-month) %>% 
  as.matrix() 

rownames(m) <- month.abb

hc3 <- hchart(m) %>% 
  hc_colorAxis(stops = color_stops(10, viridis(10))) %>% 
  hc_yAxis(title = list(text = NULL))

hc3

With the color scale used is not that clear the impact about the incremet. We can see the series have and increase but with colors is not so easy to quantify that change.

Line, time series

Let’s try now the most simply chart. And let’s represent the data as a time series.

dsts <- df %>% 
  mutate(name = paste(decade, month)) %>% 
  select(x = tmpstmp, y = median, name)

hc4 <- highchart(type = "stock") %>% 
  hc_xAxis(type = "datetime") %>%
  hc_add_series_df(dsts, name = "Global Temperature",
                   type = "line", color = hex_to_rgba("#90ee7e", 0.5),
                   lineWidth = 1,
                   states = list(hover = list(lineWidth = 1)),
                   shadow = FALSE) %>% 
  hc_rangeSelector(enabled = FALSE)

hc4

May be it’s so simple. What do you think?

Columrange

Finally let’s add the information about the confidence interval and add the media information using a color same as hrbrmstr did.

With highcharter it’s easy. Just define the dataframe with x, low, high and color and add it to a highchart object with the hc_add_series_df function.

dscr <- df %>% 
  mutate(name = paste(decade, month)) %>% 
  select(x = tmpstmp, low = lower, high = upper, name, color = color_m)

hc5 <- highchart() %>% 
  hc_xAxis(type = "datetime") %>%
  hc_add_series_df(dscr, name = "Global Temperature",
                   type = "columnrange")

hc5

(IMHO) This is the best way to show what we want to say:

Beatiful

Do you have other ways to represent this data?